package com.xindian.android.base.xda_base;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.thinkdit.lib.util.StringUtil;
import com.xindian.android.base.livepushsdk.StarLiveFragment;
import com.xindian.android.base.livepushsdk.listener.OnStarLiveCallBack;
import com.xindian.android.base.livepushsdk.util.LivePushUtil;
import com.xindian.android.base.xda_base.databinding.ActivityDemoLiveCameraBinding;

/**
 * Created by liujianjiu on 16-9-26.
 *
 * 推流实例,检测封装的推流sdk
 */

public class DemoLivePushActivity extends Activity implements View.OnTouchListener, OnStarLiveCallBack {

    private ActivityDemoLiveCameraBinding mActivityDemoLiveCameraBinding;
    private StarLiveFragment mStarLiveFragment;
    //芒果直播账号pm63@hifly.tv。观看推流地址:rtmp://liveshow.cdn.max.mgtv.com/mglive/19d0718594f9805d6f70ae04530b289c
    private static final String PUSH_URL = "rtmp://liveshow.push.max.mgtv.com/mglive/19d0718594f9805d6f70ae04530b289c?auth_key=1477299210-0-0-c046c500e6810a8d0144a225e3017772";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //趣拍初始化,鉴权
        LivePushUtil.iniAndAuthPush(this,"2092fbc0c30f4a6","67fba4d315964a2cb072ccc2bdebbcc9");
        //初始化View
        initView();
    }

    private void initView(){
        mActivityDemoLiveCameraBinding = DataBindingUtil.setContentView(this,R.layout.activity_demo_live_camera);
        mActivityDemoLiveCameraBinding.toggleCamera.setOnCheckedChangeListener(_CameraOnCheckedChange);
        mActivityDemoLiveCameraBinding.toggleFlashLight.setOnCheckedChangeListener(_SwitchFlashLightOnCheckedChange);
        mActivityDemoLiveCameraBinding.btnSwitchBeauty.setOnCheckedChangeListener(_SwitchBeautyOnCheckedChange);
        mActivityDemoLiveCameraBinding.toggleLivePush.setOnCheckedChangeListener(_PushOnCheckedChange);
        mActivityDemoLiveCameraBinding.getRoot().setOnTouchListener(this);
    }

    /**
     * 开始直播推流
     * @param pushUrl 推流地址
     */
    private void startPush(String pushUrl){
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if(mStarLiveFragment == null){
            mStarLiveFragment = new StarLiveFragment();
        }
//        //设置推理参数
        mStarLiveFragment.setPushAtrr(StarLiveFragment.OUTPUT_RESOLUTION_480P,1000000);
        mStarLiveFragment.setUrl(pushUrl);
        mStarLiveFragment.setCallBack(this);
        fragmentTransaction.replace(R.id.frame_surface_view, mStarLiveFragment);
        fragmentTransaction.commit();
    }

    /**
     * 停止直播推流
     */
    private void stopPush(){
        if(mStarLiveFragment != null){
            mStarLiveFragment.stopLive();
        }
    }


    /**
     * 刷新推流地址
     *
     * @param url url
     */
    public void flushUrl(String url) {
        if (StringUtil.isNullorEmpty(url)) {
            Toast.makeText(this, "推流地址不能为空" , Toast.LENGTH_SHORT).show();
        } else {
            mStarLiveFragment.setUrl(url);
            mStarLiveFragment.restartRecorder();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (mStarLiveFragment != null && mStarLiveFragment.isAdded()) {
            mStarLiveFragment.onTouch(event);
        }
        return false;
    }

    //闪光灯切换
    private final CompoundButton.OnCheckedChangeListener _SwitchFlashLightOnCheckedChange =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mStarLiveFragment.changeFlashModel();
                }
            };
    //美颜切换
    private final CompoundButton.OnCheckedChangeListener _SwitchBeautyOnCheckedChange =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mStarLiveFragment.changeBeauty();
                }
            };

    //镜头切换
    private final CompoundButton.OnCheckedChangeListener _CameraOnCheckedChange =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mStarLiveFragment.changeCamera();
                }
            };

    //手动推流开关
    private final CompoundButton.OnCheckedChangeListener _PushOnCheckedChange =
            new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        startPush(PUSH_URL);
                    }else{
                        stopPush();
                    }
                }
            };


    //TODO
    @Override
    public void onAuthFailure() {

    }

    @Override
    public void onStarLiveError() {

    }

    @Override
    public void onStarLiveStart() {

    }

    @Override
    public void onStarLiveNetworkChange(NetworkState state) {

    }
}
